﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using HashFoo.SqlServer.Spatial;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace HashFoo.SqlServer.SpatialTests
{
    /// <summary>
    /// Geography tests that focus on simple information/metadata functionality
    /// </summary>
    [TestClass]
    public class GeographyInfoTests
    {
        [TestMethod]
        public void ringat_returns_ring_if_exists()
        {
            var geography = Geography.FromWkt(WktResources.Polygon);

            var ring = geography.RingAt(1);

            Assert.IsNotNull(ring);
            Assert.AreEqual(GeometryType.LineString, ring.GeometryType);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void ringat_throws_if_index_out_of_range()
        {
            var geography = Geography.FromWkt(WktResources.Polygon);

            geography.RingAt(100);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void ringat_throws_if_index_is_zero()
        {
            var geography = Geography.FromWkt(WktResources.Polygon);

            geography.RingAt(0);
        }

        [TestMethod]
        public void ringat_returns_null_if_geography_has_no_rings()
        {
            var geography = Geography.FromWkt(WktResources.Point);

            var ring = geography.RingAt(1);

            Assert.IsNull(ring);
        }

        [TestMethod]
        public void equal_spatial_check_of_geographies_of_same_spatial_composition_returns_true()
        {
            var geog1 = Geography.CreateSquarePolygon(5, 5, 10, 10);
            var geog2 = Geography.CreateSquarePolygon(5, 5, 10, 10);

            Assert.IsTrue(geog1.EqualsSpatially(geog2));
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void equal_check_on_null_geography_throws()
        {
            var geog1 = Geography.CreateSquarePolygon(5, 5, 10, 10);

            Assert.IsTrue(geog1.EqualsSpatially(null));
        }

        [TestMethod]
        public void centroid_returned_for_polygon()
        {
            var geog1 = Geography.CreateSquarePolygon(5, 5, 10, 10);

            var centroid = geog1.Centroid;

            Assert.IsTrue(Math.Abs(centroid.X - 7.5) < .01);
            Assert.IsTrue(Math.Abs(centroid.X - 7.5) < .01);
        }
    }
}
